package com.myeasyexcel.utils;

import com.alibaba.excel.metadata.Head;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.write.handler.CellWriteHandler;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.alibaba.excel.write.metadata.holder.WriteTableHolder;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.xssf.streaming.SXSSFSheet;

import java.util.List;

/**
 * @author zhushuangshuang
 * @description: 设置自动适用宽度
 * @date 2022/4/27 16:08
 */
public class AutoColumnWidthHandler implements CellWriteHandler {
    @Override
    public void afterCellDispose(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, List<WriteCellData<?>> cellDataList, Cell cell, Head head, Integer relativeRowIndex, Boolean isHead) {
        SXSSFSheet sheet = (SXSSFSheet) writeSheetHolder.getSheet();
        sheet.trackAllColumnsForAutoSizing();
        sheet.autoSizeColumn(cell.getColumnIndex());
    }


}
